<template>
	<view class="content wrap">
		<u-form :model="form" ref="uForm"  :rules="rules">
			<u-form-item label="项目部" label-width="160" left-icon="home"><u-input v-model="form.operateName" type="select" @click="getO()"/></u-form-item>
			
			<u-form-item label="客户名称" label-width="170" left-icon="account"><u-input v-model="form.customerName" type="select" @click="getC()"/></u-form-item>
			
			<u-form-item label="库位" label-width="170"><u-input v-model="form.positionNumber" type="select" @click="getp()" placeholder="请选择"/></u-form-item>
			
			<u-form-item label="商品编号" label-width="170"><u-input v-model="form.goodsNumber" type="select" @click="getg()" placeholder="请选择"/></u-form-item>
			
			<u-form-item label="商品名称" label-width="170"><u-input v-model="form.goodsName" type="select" @click="getg()" placeholder="请选择"/></u-form-item>
			
			<u-form-item label="数量" label-width="150" prop="num"><u-input v-model="form.num" type="digit" placeholder="请输入数量"/></u-form-item>
			
			<u-form-item label="生产日期" label-width="170" left-icon="calendar"><u-input v-model="form.producetime" type="select" @click="getT()"/></u-form-item>
			
			<u-form-item label="批次" label-width="170"><u-input v-model="form.goodsbatchs"/></u-form-item>
			
			<u-form-item label="入库批次" label-width="170" left-icon="calendar"><u-input v-model="form.putTime" type="select" @click="getT2()"/></u-form-item>
			
		</u-form>
		<u-button @click="submit">提交</u-button>
		<view>
			<u-select v-model="show2" :list="olist" @confirm="confirm2"></u-select>
		</view>
		<view>
			<u-select v-model="show" :list="clist" @confirm="confirm"></u-select>
		</view>
		<view>
			<u-select v-model="show3" mode="mutil-column-auto" :list="plist" @confirm="confirm3"></u-select>
		</view>
		<u-popup v-model="show4" mode="center" closeable width="550rpx" height="600rpx" border-radius="10">
			<view class=" u-p-r-30 u-p-l-30 u-m-t-20" style="margin-top: 40rpx;">
				<u-search placeholder="请输入关键词搜索" :clearabled="true" v-model="keyword" border-color="#606266" @custom="search" @search="search"></u-search>
			</view>
			<view class="wrap">
				<u-row v-for="item in glist" :key="item.id" @click="xuanzegoods(item)">
					<u-col span="12">
						<view class="title u-m-t-20">
							<span>{{item.goodsNumber}},{{item.goodsName}},{{item.goodsNo}}</span>
						</view>
					</u-col>
				</u-row>
			</view>
		</u-popup>
		<view>
			<u-calendar v-model="show5" mode="date" max-date="2070-01-01" @change="change"></u-calendar>
		</view>
		<view>
			<u-calendar v-model="show6" mode="date" max-date="2070-01-01" @change="change2"></u-calendar>
		</view>
		<view>
			<u-toast ref="uToast" />
		</view>
	</view>
</template>
<script>
	export default {
		data() {
			return {
				keyword:'',
				form:{
				    operateId:'',
				    operateName:"",
					customerId:"",
				    customerName:"",
				    warehouseId:"",
				    warehouseName:"",
				    areaId:"",
				    areaNumber:"",
				    positionId:"",
				    positionNumber:"",
				    goodsId:"",
				    goodsName:"",
				    goodsNumber:"",
				    num:"",
				    putTime:"",
					producetime:"",
					goodsbatchs:""
				},
				olist:[],
				clist:[],
				plist:[],
				glist:[],
				show:false,
				show2:false,
				show3:false,
				show4:false,
				show5:false,
				show6:false,
			    rules: {
					num: [
						{ 
							required: true,
							message: '请输入数量', 
							trigger: 'blur',
						},
						{
							type:'number',
							message: '请输入正确的数据类型', 
							trigger: 'blur'
						}
					],
				}
			};
		},
		onLoad() {
			let year = new Date().getFullYear();//年
			let month = new Date().getMonth() + 1;//注意！月份是从0月开始获取的，所以要+1;
			let day = new Date().getDate();//日
			this.form.putTime =  year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day);
			this.form.producetime =  year + '-' + (month >= 10 ? month : '0' + month) + '-' + (day >= 10 ? day : '0' + day);
			this.$http.post('/rest/operate/list',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.form.operateId = res.data.data[0].id
				this.form.operateName = res.data.data[0].operateName
				for(let item1 of res.data.data) {
					let obj = {}
					obj.value = item1.id
					obj.label = item1.operateName
					this.olist.push(obj)
				}
			})
			this.$http.post('/rest/customer/list',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.form.customerId = res.data.data[0].id
				this.form.customerName = res.data.data[0].customerName
				for(let item1 of res.data.data) {
					let obj = {}
					obj.value = item1.id
					obj.label = item1.customerName
					this.clist.push(obj)
				}
			})
			this.$http.post('/rest/storage/getWarehouselist',{
			},{
				params: {},
				header: {
					'Authorization' :uni.getStorageSync('token'),
				}
			}).then(res => {
				this.plist = res.data.data
			})
		},
		onReady() {
			this.$refs.uForm.setRules(this.rules);
		},
		methods:{
			getT(){
				this.show5 = true
			},
			change(e){
				this.form.producetime = e.result
			},
			getT2(){
				this.show6 = true
			},
			change2(e){
				this.form.putTime = e.result
			},
			xuanzegoods(item){
				this.form.goodsId = item.id
				this.form.goodsName = item.goodsName
				this.form.goodsNumber = item.goodsNumber
				this.show4 = false
			},
			search(){
				this.$http.post('/rest/storage/goodslist',{
				},{
					params: {
						condition:this.keyword,
						goodsCustomerId:this.form.customerId
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let arr = []
					let inta = 0
					for(let item1 of res.data.data) {
						if(inta != 10){
							arr.push(item1)
						} else {
							break
						}
						inta++
						let obj = {}
					}
					this.glist = arr
				})
			},
			getC(){
				this.show = true
			},
			getO(){
				this.show2 = true
			},
			getp(){
				this.show3 = true
			},
			getg(){
				this.$http.post('/rest/storage/goodslist',{
				},{
					params: {
						condition:this.keyword,
						goodsCustomerId:this.form.customerId
					},
					header: {
						'Authorization' :uni.getStorageSync('token'),
					}
				}).then(res => {
					let arr = []
					let inta = 0
					for(let item1 of res.data.data) {
						if(inta != 6){
							arr.push(item1)
						} else {
							break
						}
						inta++
						let obj = {}
					}
					this.glist = arr
				})
				this.show4 = true
			},
			confirm(e) {
				this.form.customerId = e[0].value
				this.form.customerName = e[0].label
			},
			confirm2(e) {
				this.form.operateId = e[0].value
				this.form.operateName = e[0].label
			},
			confirm3(e) {
				this.form.warehouseId = e[0].value
				this.form.warehouseName = e[0].label
				this.form.areaId = e[1].value
				this.form.positionId = e[2].value
				this.form.positionNumber = e[2].label
			},
			submit() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						console.log('验证通过');
						if(typeof this.form.operateId == 'undefined' || this.form.operateId == null || this.form.operateId == ""){
							uni.showToast({
								title: "请选择项目部",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.customerId == 'undefined' || this.form.customerId == null || this.form.customerId == ""){
							uni.showToast({
								title: "请选择客户",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.positionId == 'undefined' || this.form.positionId == null || this.form.positionId == ""){
							uni.showToast({
								title: "请选择库位",
								icon: 'none'
							})
							return
						}
						if(typeof this.form.goodsName == 'undefined' || this.form.goodsName == null || this.form.goodsName == ""){
							uni.showToast({
								title: "请选择商品",
								icon: 'none'
							})
							return
						}
						uni.showLoading({
							title: '正在提交'
						})
						this.$http.post('/rest/storage/addStorage',{
						},{
							params: this.form,
							header: {
								'Authorization' :uni.getStorageSync('token'),
							}
						}).then(res => {
							uni.hideLoading()
							if(res.data.code == 200) {
								this.$refs.uToast.show({
									title: '添加成功',
									type: 'success'
								})
								setTimeout(() => {
									this.form.num = ''
								}, 1000)
							} else {
								this.$refs.uToast.show({
									title: res.data.message,
									type: 'error'
								})
							}
						})
					} else {
						console.log('验证失败');
					}
				});
			},
		}
	}
</script>
<style lang="scss">
	page{
			background-color: #ffffff;
	}
	.wrap {
		padding: 30rpx;
	}
	.title{
		font-size: 28rpx;
		font-weight: 600;
		color: #2b85e4;
	}
</style>
